local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.BaseObject())
local CallBackManager = ClientOHOH.load.CallBackManager();
local ConnectClass=_G.CommonOHOH.load.Connect();
local TweenHelper = _G.CommonOHOH.load.TweenHelper();

local tw1 = TweenInfo.new(0.3, Enum.EasingStyle.Back,Enum.EasingDirection.Out);
local tw2 = TweenInfo.new(0.1, Enum.EasingStyle.Back,Enum.EasingDirection.Out);
ClientOHOH.load.PetCell()

local instStr={
    root="ctrl.List.root",--List根节点
    layout ="ctrl.List.layout",--表格控制器
    cell="ctrl.List.cell",--单元预制体
}

function M.new(...)
	return M(...);
end;

function M.ctor(self,ctrl)
    M.super.ctor(self);
    self.ctrl=ctrl
    self.List={}
    self.ui=ctrl:InitUiInst2(ctrl.main_inst,instStr)
    self.ui.cell.Visible=false
    self.EquipCount=0

    self:Big()
end;

function M.dtor(self)
	M.super.dtor(self);
end;



function M.GetGuid(self,petId,index)
    return petId.."_"..index
end

function M.CreateCell(self,guid,petId)
    local cell=  require(script.Parent.Cell).new(petId,guid,self.ui.cell.Parent, self.ctrl:GetDataBase())
    cell.ClickEvent:Connect(function()
        self.ctrl:CellClick(cell)
    end)
    return cell
    -- return   ClientOHOH.load.PetCell().new(petId,guid,self.ui.cell.Parent, self.ctrl:GetDataBase())
end

---------------------------- 公共函数----------------------------------------------
function M.UpdateCell(self,data)

    local had={} --记录已经拥有的
    for  petId,count in pairs(data) do
         for i=1,count do
            -- print(i)
            local guid=self:GetGuid(petId,i)
            had[guid]=true
            if  not self.List[guid] then --不存在，需要创建新的
                self.List[guid]=self:CreateCell(guid,petId)
            end
         end
    end

    for guid,cell in pairs(self.List) do
        if not had[guid] then --不存在
            cell:dtor() --销毁
            self.List[guid]=nil
        end
    end

end

function M.UpdateEquip(self,data)
    self.EquipCount=0
    for guid,cell in pairs(self.List) do
        cell:UnEquip()
    end
    for  petId,count in pairs(data) do
        for i=1,count do
           local guid=self:GetGuid(petId,i)
            local cell=self.List[guid]
            assert(cell,"不存在guid:"..guid)
            cell:Equip()
           self.EquipCount= self.EquipCount+1
        end
   end
end

function M.GetEquipCount(self)
    return self.EquipCount
end

function M.GetAllCell(self)
    return self.List
end


--列表变大
function M.Big(self)
    self.ui.layout.CellSize=self.ui.layout:GetAttribute("big")
    TweenHelper:PlayTween(self.ui.root, tw1, {
        Size=self.ui.root:GetAttribute("big")
    });
end

--列表变小
function M.Small(self)
    TweenHelper:PlayTween(self.ui.root, tw1, {
        Size=self.ui.root:GetAttribute("small")
    });
    self.ui.layout.CellSize=self.ui.layout:GetAttribute("small")
end

return M